﻿using UnityEngine;
using System.Collections;

[AddComponentMenu("UITools/ShowMsgError")]
public class UIShowMsgError : MonoBehaviour
{
	static public UIShowMsgError Instance;
	
	void Awake () {
		if (Instance == null)
			Instance = this; 
	}
	void OnDestroy () 
	{
		NetworkDispatcher.Instance.OnShowErrorInfo = null;
		Instance = null; 
	}
	
	public GameObject MsgBox;
	public GameObject MsgMoney;
	public GameObject MsgMansion;

	// Use this for initialization
	void Start ()
	{
		NetworkDispatcher.Instance.OnShowErrorInfo += OnShowErrorInfoEx;
	}
	
	// Update is called once per frame
	void Update ()
	{
	
	}
	
	public void OnShowErrorInfoEx(uint ret_code, string msg)
	{		
		if (!string.IsNullOrEmpty(msg))
		{
			ShowErrorInfo(msg);
		}
		else
		{
			string error = NetMsgError.Instance().GetError(ret_code);
			if (error != null)
			{
				ShowErrorInfo(error);
			}
		}
	}
	
	public void OnShowErrorInfo(uint ret_code)
	{
		string error = NetMsgError.Instance().GetError(ret_code);
		if (error != null)
		{
			ShowErrorInfo(error);
		}
	}
	
	public void ShowErrorInfo(string error)
	{
		if (MsgBox == null)
			return;
		
		GameObject go = GameObject.Instantiate(MsgBox) as GameObject;
		go.transform.parent = transform;
		go.transform.localPosition = new Vector3(0, 0, 0);
		go.transform.localScale = new Vector3(1, 1, 1);
		
//		go.GetComponent<SelfDestory>().enabled = true;
//		go.GetComponent<Msgbx>().Msg.text = error;
	}
}

